package com.snake.win.obj;

import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.snake.win.utils.GameUtils;

import java.util.Random;

public class FoodObj extends GameObj {

    //随机函数
    Random r = new Random();

    public FoodObj() {
        super();
    }

    public FoodObj(Texture image, int x, int y) {
        super(image, x, y);
    }

    public FoodObj(int x, int y) {
        super(x, y);
    }

    //获取食物
    public FoodObj getFood(Snake snake) {
        FoodObj food = new FoodObj(GameUtils.foodImg, (r.nextInt(20) + 1), (r.nextInt(20) + 1));
        if (food.snake_x == snake.headObj.snake_x && food.snake_y == snake.headObj.snake_y) {
            food = new FoodObj().getFood(snake);
        }
        for (GameObj bodyObj : snake.bodyObjList) {
            if (food.snake_x == bodyObj.snake_x && food.snake_y == bodyObj.snake_y) {
                food = new FoodObj().getFood(snake);
                break;
            }
        }
        return food;
    }

    @Override
    public void paintSelf(SpriteBatch g) {
        sprite.setPosition(snake_x, snake_y);
        super.paintSelf(g);
    }
}
